/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.yournamehere.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.*;
/**
 *
 * @author D4v1D
 */
public class SaverPanel extends VerticalPanel{
        private int gameTime;
        private Label timeLabel;
        private TextBox userName;
        private SudokuPanel p;
        public SaverPanel(SudokuPanel p){
            this.p=p;
            gameTime=0;
            timeLabel=new Label();
            Timer t = new Timer() {

                @Override
                public void run() {
                    if (SaverPanel.this.p.isComplete()) {

                        Label winLabel = new Label();
                        winLabel.setText("You won in " + Integer.toString(gameTime/60)
                            + "'" + Integer.toString(gameTime%60) +"''");
                        
                    }else{
                        gameTime++;
                        timeLabel.setText("Time : " + Integer.toString(gameTime/60)
                                + "'" + Integer.toString(gameTime%60) + "''");
                        this.schedule(1000);
                    }
                }
            };
            t.schedule(1000);
            this.add(timeLabel);
            userName = new TextBox();
            this.add(userName);
            // Create an asynchronous callback to handle the result.
            final AsyncCallback callback = new AsyncCallback() {
                public void onSuccess(Object result) {
                    new SudokuDialog((String) result);
                }

                public void onFailure(Throwable caught) {
                    new SudokuDialog("Communication failed");
                }
            };
            this.add(new Button("Save result", new ClickListener() {
              public void onClick(Widget sender) {
                  if(SaverPanel.this.p.isComplete()){
                        getService().myMethod(SaverPanel.this.userName.getText(),"",timeLabel.getText(), callback);
                  }else{
                      new SudokuDialog("The game is not finished !");
                  }
              }
            }));
        }
        public static SaverServiceAsync getService(){
        // Create the client proxy. Note that although you are creating the
        // service interface proper, you cast the result to the asynchronous
        // version of
        // the interface. The cast is always safe because the generated proxy
        // implements the asynchronous interface automatically.
        SaverServiceAsync service = (SaverServiceAsync) GWT.create(SaverService.class);
        // Specify the URL at which our service implementation is running.
        // Note that the target URL must reside on the same domain and port from
        // which the host page was served.
        //
        ServiceDefTarget endpoint = (ServiceDefTarget) service;
        String moduleRelativeURL = GWT.getModuleBaseURL() + "saverservice";
        endpoint.setServiceEntryPoint(moduleRelativeURL);
        return service;
    }
}

